$(function(){
	page.bind();
	page.getAllType();
	page.getAllMessageType();
	page.getAllDept();
});


var typeTable;
var tableUser;
var tableAddUser;
//页面总变量
var page = {
		userIdArray:[],
		bind:function(){
			$("#userAddTable").on("click","input[name=addUserCheck]",function(){
				if($(this).prop("checked")){
					page.userIdArray.push($(this).val());
				}else{
					var index= page.userIdArray.indexOf($(this).val());
					page.userIdArray.splice(index, 1);
				}
				layer.msg("当前选择人数为"+page.userIdArray.length,{icon:1});
			});
			
			//点击页面添加按钮
			$("#addTypeBtn").bind("click",function(){
				$("#addTypeModal").modal("show");
			});
			
			//部门改变时
			$("#deptId").bind("change",function(){
				var deptId=$(this).val();
				nameToIdUtil.getWorkShopNameAndId("workShopId",deptId);
			});
			
			
			//点击模态框添加按钮
			$("#addTypeModalBtn").bind("click",function(){
				var sendDate=new Object();
				sendDate.type=$("#type option:selected").val();
				sendDate.deptId=$("#deptId option:selected").val();
				sendDate.workShopId=$("#workShopId option:selected").val();
				
				ajaxUtil.ajaxRuquest("weChatToMessage/addMessageType",sendDate,function(data){
					layer.msg(data,{icon:1});
					$("#addTypeModal").modal("hide");
					typeTable.ajax.reload(null,false);
				});
			});
			
			
			/**
			 *  类型下的用户信息
			 */
			$("#typeTable").on("click",".selectUser",function(){
				var id=$(this).val();
				var type=$(this).parent().prev().prev().prev().text();
				var deptName=$(this).parent().prev().prev().text();
				var workShopName=$(this).parent().prev().text();
				$("#userModalTypeId").val(id);
				$("#thisReport").text(type +"  "+ deptName +"  "+ workShopName);
				page.getUser(id);
				$("#userModal").modal("show");
			});
			
			
			$("#userQueryUserBtn").bind("click",function(){
				var id=$("#userModalTypeId").val();
				page.getUser(id);
			});
			
			
			/**
			 *  添加类型用户
			 */
			$("#typeTable").on("click",".addUser",function(){
				page.userIdArray=[];
				var id=$(this).val();
				var type=$(this).parent().prev().prev().prev().text();
				var deptName=$(this).parent().prev().prev().text();
				var workShopName=$(this).parent().prev().text();
				$("#addUserModalTypeId").val(id);
				$("#addUserTitle").text(type +"  "+ deptName +"  "+ workShopName);
				page.getAddUser(id);
				$("#addUserModal").modal("show");
			});
			
			
			/**
			 *  删除类型
			 */
			$("#typeTable").on("click",".deleteType",function(){
				var id=$(this).val();
				ajaxUtil.ajaxRuquest("weChatToMessage/deleteType",{'id':id},function(data){
			        layer.msg(data,{icon:1});
			        typeTable.ajax.reload(null, false);
				});
			});
			
			
			//添加人员查询人员
			$("#addUserQueryUserBtn").bind("click",function(){
				var id =$("#addUserModalTypeId").val();
				page.getAddUser(id);
			});
			
			//添加人员模态框确认按钮
			$("#addUserModalBtn").bind("click",function(){
				var id=$("#addUserModalTypeId").val();
				var userIdArray=page.userIdArray;
				if(userIdArray.length<=0){
					layAlert("请选择要添加人员");
					return false;
				}
				ajaxUtil.ajaxRuquest("weChatToMessage/addTypeUser",{'id':id,'userIdArray':JSON.stringify(userIdArray)},function(data){
			        layer.msg(data,{icon:1});
			        page.userIdArray=[];
				});
			});
			
			/**
			 * 移除单个用户
			 */
			$("#userTable tbody").on("click",".removeUser",function(){
				var id=$("#userModalTypeId").val(); 
				var userId=$(this).val();
				ajaxUtil.ajaxRuquest("weChatToMessage/removeTypeUser",{'id':id,'userId':userId},function(data){
			        layer.msg(data,{icon:1});
			        page.getUser(id);
				});
			});
		},
		getAllMessageType:function(){
			typeTable=ajaxUtil.dataTablesAjax("typeTable",10,'weChatToMessage/getAllMessageType',
					{},[
					{"data":"typeName"},
					{"data":"deptName"},
					{"data":"workShopName"},
					{"data":"id",render: function (data,type,row) {
						return "<button class='deleteType btn btn-link' value='"+data+"'>删除</button>" +
						 "<button class='selectUser btn btn-link' value='"+data+"'>查看人员</button>" +
						 "<button class='addUser btn btn-link' value='"+data+"'>添加人员</button>"  ;
					}}
			]);
		},
		getAllType:function(){
			ajaxUtil.ajaxRuquest("weChatToMessage/getAllType",null,function(data){
				var html="";
				for(var i=0;i<data.length;i++){
					html+="<option value='"+data[i].id+"'>"+data[i].typeName+"</option>";
				}
				$("#type").html(html);
			});
		},
		getAllDept:function(){
			ajaxUtil.ajaxRuquest("basicData/getDeptName",{'view':1},function(data){
				var html="<option value=''>请选择</option>";
				for(var i=0;i<data.length;i++){
					html+="<option value='"+data[i].id+"'>"+data[i].deptName+"</option>";
				}
				$("#deptId").html(html);
			});
		},
		getUser:function(id){
			var serachWhere=$("#userSerachWhere").val();
			tableUser=ajaxUtil.dataTablesAjaxNotFxiaction("userTable",10,"weChatToMessage/getTypeUser",
					{"id":id,'serachWhere':serachWhere},[
						{"data":"userName"},
						{"data":"deptName"},
						{"data":"workShopName"},
						{"data":"phone"},
						{"data":"id",render: function (data,type,row) {
							return "<button class='removeUser btn btn-link' value='"+data+"' >移除</button>";
						}}
					]);
		},
		getAddUser:function(id){
			var serachWhere=$("#addUserSerachWhere").val();
			tableAddUser=ajaxUtil.dataTablesAjaxNotFxiaction("userAddTable",10,"weChatToMessage/getTypeNoHaveUser",
					{"serachWhere":serachWhere,'id':id},[
						{"data":"userName"},
						{"data":"deptName"},
						{"data":"workShopName"},
						{"data":"phone"},
						{"data":"id",render: function (data,type,row) {
							var checked="";
							if(page.userIdArray.indexOf(data)!=-1){
								checked="checked";
							}
							return "<input type='checkbox'  name='addUserCheck' value='"+data+"' "+checked+" >";
						}}
						]);
		}
}